package demo1;

import javax.swing.tree.TreeNode;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: liangyuanzhang
 * Date: 2025-4-19
 * Time: 10:07
 */
public class Test {
    //获取叶子节点的个数 --> 运用了1.循环遍历 2.子问题
    public int LeafNodeCount = 0;
    public void getLeafNodeCount(TreeNode root){
        if (root == null)
            return;
        if (root.left == null && root.right == null)
            LeafNodeCount++;
        getLeafNodeCount(root.left);
        getLeafNodeCount(root.right);
    }
    public int getLeafNodeCount2(TreeNode root){
        if (root == null)
            return 0;
        if (root.left == null && root.right == null)
            return 1;
        return getLeafNodeCount2(root.left) + getLeafNodeCount2(root.right);
    }
}
